<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="ant-design:down-outlined" />
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <ProcessDesignModal @register="registerAddModal" @success="handleSuccess" />
    <DesignModal @register="registerDesignModal" @success="handleSuccess" />
    <PreviewDesignModal @register="registerPreviewModal" />
    <FormDesigner ref="formDesignerRef" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" name="system-position" setup>
  import { ref } from 'vue';
  import { BasicTable, TableAction, ActionItem } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import {
    list,
    deleteProcessDesign,
    batchDeleteProcessDesign,
    deployProcessDesign,
    redeployProcessDesign,
    getExportUrl,
    getImportUrl,
  } from './processdesign.api';
  import { columns, searchFormSchema } from './processdesign.data';
  import ProcessDesignModal from './ProcessDesignModal.vue';
  import DesignModal from './DesignModal.vue';
  import PreviewDesignModal from './PreviewDesignModal.vue';
  import FormDesigner from './formDesigner/index.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useListPage } from '/@/hooks/system/useListPage';

  const formDesignerRef = ref();

  const { createMessage } = useMessage();
  const [registerAddModal, { openModal }] = useModal();
  const [registerDesignModal, { openModal: openDesignModel }] = useModal();
  const [registerPreviewModal, { openModal: openPreviewModel }] = useModal();

  // 列表页面公共参数、方法
  const { prefixCls, onExportXls, onImportXls, tableContext } = useListPage({
    designScope: 'processDesign-template',
    tableProps: {
      title: '流程设计列表',
      api: list,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 200,
      },
      showIndexColumn: true,
    },
    exportConfig: {
      name: '流程设计列表',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '查看',
        onClick: handlePreview.bind(null, record),
        auth: ['admin', 'wf:processDesign:detail'], // 根据权限控制是否显示: 无权限，不显示
      },
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        auth: ['admin', 'wf:processDesign:update'], // 根据权限控制是否显示: 无权限，不显示
      },
      {
        label: '设计',
        onClick: handleDesign.bind(null, record),
        auth: ['admin', 'wf:processDesign:updateDefine'], // 根据权限控制是否显示: 无权限，不显示
      },
    ];
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record): ActionItem[] {
    return [
      {
        label: '表单设计',
        // disabled: record.jsonObject?.instanceUrl != 'schemaWfForm', // 元数据表单才能编辑
        auth: ['admin', 'wf:processDesign:updateDefine'], // 根据权限控制是否显示: 无权限，不显示
        onClick: handleOpenFormDesigner.bind(null, record),
      },
      {
        label: '部署',
        disabled: record.isDeployed == 1,
        popConfirm: {
          title: '部署会生成新的流程定义版本，确定部署吗？',
          confirm: handleDeploy.bind(null, record),
        },
        auth: ['admin', 'wf:processDesign:deploy'], // 根据权限控制是否显示: 无权限，不显示
      },
      {
        label: '重新部署',
        disabled: record.isDeployed == 1,
        popConfirm: {
          title: '重新部署会覆盖最新版流程定义，确定重新部署吗？',
          confirm: handleRedeploy.bind(null, record),
        },
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  // 流程设计保存成功，刷新列表
  function handleSuccess() {
    reload();
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
    });
  }

  /**
   * 编辑事件
   */
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  /**
   * 流程设计预览事件
   */
  function handlePreview(record) {
    openPreviewModel(true, {
      record,
    });
  }
  /**
   * 流程设计事件
   * @param {} record
   */
  const handleDesign = (record) => {
    openDesignModel(true, {
      record,
    });
  };
  /**
   * 表单设计事件
   */
  function handleOpenFormDesigner(record) {
    formDesignerRef.value.show(record);
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteProcessDesign({ id: record.id }, reload);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteProcessDesign({ ids: selectedRowKeys.value }, reload);
  }

  /**
   * 流程设计定义部署
   * @param {*} record
   */
  function handleDeploy(record) {
    deployProcessDesign({
      id: record.id,
    }).then(() => {
      createMessage.success('部署成功');
      reload();
    });
  }

  /**
   * 流程设计定义重新部署
   * @param {*} record
   */
  function handleRedeploy(record) {
    redeployProcessDesign({
      id: record.id,
    }).then(() => {
      createMessage.success('重新部署成功');
      reload();
    });
  }
</script>
